package com.ideal.view.settings;
import com.ideal.core.constant.Type;
import org.jdom.Element;
import com.intellij.openapi.components.*;
import org.jetbrains.annotations.Nullable;

/**
 * @author
 */
@State(name = "TemplateSetting", storages = {
            @Storage(id="other",file = StoragePathMacros.APP_CONFIG+"/generator.xml")
    }
)
public class TemplateSetting implements PersistentStateComponent<Element> {

    private static TemplateBaseInfo info = new TemplateBaseInfo();

    public static TemplateSetting getInstance() {
        return  ServiceManager.getService(TemplateSetting.class);
    }

    @Nullable
    @Override
    public Element getState() {
        Element element = new Element("TemplateConfiguration");
        element.setAttribute(Type.CONTROLLER.toString(),this.getInfo().getController());
        element.setAttribute(Type.SERVICE.toString(),this.getInfo().getService());
        element.setAttribute(Type.SERVICE_IMPL.toString(),this.getInfo().getServiceImpl());
        element.setAttribute(Type.DAO.toString(),this.getInfo().getDao());
        element.setAttribute(Type.DAO_IMPL.toString(),this.getInfo().getDaoImpl());
        element.setAttribute(Type.DOMAIN.toString(),this.getInfo().getDomain());
        element.setAttribute(Type.DOMAIN_MAPPING.toString(),this.getInfo().getDomainMapping());

        element.setAttribute(Type.DRIVER_CLASS.toString(),this.getInfo().getDriverClass());
        element.setAttribute(Type.JDBC_URL.toString(),this.getInfo().getJdbcUrl());
        element.setAttribute(Type.USER.toString(),this.getInfo().getUser());
        element.setAttribute(Type.PASSWORD.toString(),this.getInfo().getPassword());

        element.setAttribute(Type.FORM.toString(),this.getInfo().getForm());
        return element;
    }

    @Override
    public void loadState(Element element) {
        info.setController(element.getAttributeValue(Type.CONTROLLER.toString()));
        info.setService(element.getAttributeValue(Type.SERVICE.toString()));
        info.setServiceImpl(element.getAttributeValue(Type.SERVICE_IMPL.toString()));
        info.setDao(element.getAttributeValue(Type.DAO.toString()));
        info.setDaoImpl(element.getAttributeValue(Type.DAO_IMPL.toString()));
        info.setDomain(element.getAttributeValue(Type.DOMAIN.toString()));
        info.setDomainMapping(element.getAttributeValue(Type.DOMAIN_MAPPING.toString()));

        info.setDriverClass(element.getAttributeValue(Type.DRIVER_CLASS.toString()));
        info.setJdbcUrl(element.getAttributeValue(Type.JDBC_URL.toString()));
        info.setUser(element.getAttributeValue(Type.USER.toString()));
        info.setPassword(element.getAttributeValue(Type.PASSWORD.toString()));

        info.setForm(element.getAttributeValue(Type.FORM.toString()));
    }

    public TemplateBaseInfo getInfo() {
        return info;
    }

    public static void setInfo(TemplateBaseInfo info) {
        TemplateSetting.info = info;
    }
}
